

import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

public class FirstCanvas extends Canvas implements CommandListener { 
    private Image wall, img, img2;
    private Command search;
    private Command back;
    private Command ok;
    private String title = new String("Ban be");
    public Friends[] friends;
    int           selected = 0;
    int           align    = 3;
    int           height   = 20;
    int           top      = 19;
    int           curView  = 0;
    int           rows     = (this.getHeight()-top-align)/height;
    Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL);

public FirstCanvas()
{
    
    search = new Command("Tim kiem", Command.ITEM, 0);
    back = new Command("Tro lai", Command.BACK, 0);
    ok  = new Command("Chon", Command.OK, 0);
    this.addCommand(search);
    this.addCommand(back);
    this.addCommand(ok);
    this.setCommandListener(this);
   
    Friends[] friendslist = new Friends[16];
        for (int i=0; i<16; i++){
            friendslist[i] = new Friends("Ban be online "+i);
        }
        this.loadItems(friendslist);
        try {
            wall = Image.createImage("/walls.png");
            img = Image.createImage("/online.png");
            img2 = Image.createImage("/top.png");
        } catch (Exception e){
        throw new RuntimeException("Loi tai hinh anh: "+e);
        }




}
 public void loadItems(Friends[] friendslist){
        this.friends = new Friends[friendslist.length];
        for (int i=0; i<friendslist.length; i++)
            this.friends[i] = friendslist[i];
    }




public void keyPressed(int keyCode)
{
    int gameAction = getGameAction(keyCode);
    
     switch (gameAction){
        case DOWN:
            selected++;
            if (selected>=friends.length) selected=0;
            repaint();
        }
       switch (gameAction){
        case UP:
            selected--;
            if (selected<0) selected= friends.length-1;
            repaint();
        }
       
        switch (gameAction){
        case KEY_NUM1:
            // Login
        }
         switch (gameAction){
        case KEY_NUM2:
            // Logout
        }
          switch (gameAction){
        case KEY_NUM3:
            // Chat with
        }
           switch (gameAction){
        case KEY_NUM4:
            // Chat group
        }
            switch (gameAction){
        case KEY_NUM5:
            // Send file
        }
             switch (gameAction){
        case KEY_NUM6:
            // Changr status
        }
             switch (gameAction){
        case KEY_NUM7:
            // Set Hide/Online
        }
              switch (gameAction){
        case KEY_NUM8:
            // Insert user name/Insert message
        }
               switch (gameAction){
        case KEY_NUM9:
            // ...
        }
               
           
}
       



public void paint(Graphics g)
{

g.setColor(0xffffff);

g.fillRect(0, 0, getWidth(), getHeight());

g.setColor(0xccff33);
g.fillRect(align, top+align+(selected-curView)*height, this.getWidth()-2*align, height);
g.drawImage(img2, getWidth()/2, 0, Graphics.TOP | Graphics.HCENTER);
g.drawImage(wall, getWidth() / 2, getHeight() / 2, Graphics.HCENTER | Graphics.VCENTER);
g.setColor(0x009900);
g.drawString(title, getWidth()/2, 3, Graphics.TOP | Graphics.HCENTER);
g.setColor(0x000000);

g.setFont(font);

g.setColor(0xff0000);


if (selected >= (curView+rows)) curView = selected-rows+1;
        if (selected < curView) curView = selected;

g.setColor(0x000000);


for (int i=0;i<rows;i++){
            g.drawRegion(img, 0, 0, 16, 16, Sprite.TRANS_NONE,align+height/2, top+align+i*height+height/2, Graphics.VCENTER | Graphics.HCENTER);
            
            
            g.drawString(friends[curView+i].name, align*4+height, top+align+i*height+height/2+align+1, Graphics.LEFT | Graphics.BASELINE);
           
           
        }

}

    public void commandAction(Command c, Displayable d) {
        if (c == search) {
            SetDisplay.setCurrent(SetDisplay.SEARCH);
        }
        else if (c == back) {
            SetDisplay.setCurrent(SetDisplay.MAINCANVAS);
        }
        else if (c == ok) {
            SetDisplay.setCurrent(SetDisplay.CHATROOM);
        }
    }

          
    }





 

   

